#import "FolderViewController.h"
#import "BaseViewController.h"

@implementation FolderViewController
@synthesize countLab;
@synthesize priceLab;
@synthesize isItem;
@synthesize desc;
@synthesize viewPosition;

- (void)viewDidLoad
{
    NSInteger positionY;
    //初始化背景图片
    if(isItem){
        positionY = 5;
    }
    else{
        positionY = 17;
    }
    //设置背景图片
    NSString *filePath;
    if([viewPosition isEqualToString:@"top"]){
        filePath=[[NSBundle mainBundle] pathForResource:@"bgTop" ofType:@"png"];
    }
    else if([viewPosition isEqualToString:@"middle"]){
        filePath=[[NSBundle mainBundle] pathForResource:@"btnBg" ofType:@"png"];
    }
    else if([viewPosition isEqualToString:@"bottom"]){
        filePath=[[NSBundle mainBundle] pathForResource:@"bgBottom" ofType:@"png"];
    }
    else{
        filePath=[[NSBundle mainBundle] pathForResource:@"bg" ofType:@"png"];
    }
    UIImageView* bgView=[[UIImageView alloc] initWithImage:[[UIImage imageWithContentsOfFile:filePath] stretchableImageWithLeftCapWidth:0 topCapHeight:0]];
    [self.view addSubview:bgView];
    
    if(isItem && [desc length] > 0){
        //定义显示描述的label
        UILabel *descLab = [[UILabel alloc] initWithFrame:CGRectMake(5, positionY, 30, 28)];
        descLab.font = [UIFont fontWithName:@"Helvetica-Bold" size:12];
        [descLab setText:desc];
        descLab.textAlignment =  UITextAlignmentLeft;
        [descLab setTextColor:[UIColor blackColor]];
        [descLab setBackgroundColor:[UIColor clearColor]];
        [self.view addSubview:descLab];
    }
    
    //定义显示单价的label
    priceLab = [[UILabel alloc] initWithFrame:CGRectMake(30, positionY, 57, 28)];
    priceLab.font = [UIFont fontWithName:@"Helvetica-Bold" size:12];
    priceLab.backgroundColor = [UIColor clearColor];
    priceLab.textColor = [UIColor orangeColor];
    priceLab.textAlignment = UITextAlignmentCenter;
    priceLab.adjustsFontSizeToFitWidth = NO;
    
    //定义减少按钮
    UIButton *minusButton = [[UIButton alloc] initWithFrame:CGRectMake(95, positionY, 39, 28)];
    NSString *filePath2=[[NSBundle mainBundle] pathForResource:@"btnLeft" ofType:@"png"];
    [minusButton setBackgroundImage:[UIImage imageWithContentsOfFile:filePath2] forState:UIControlStateNormal];
    [minusButton addTarget:self action:@selector(minuButtonPressd:) forControlEvents:UIControlEventTouchUpInside];
    
    //定义增加按钮
    UIButton *addButton = [[UIButton alloc] initWithFrame:CGRectMake(178, positionY, 39, 28)];
    NSString *filePath3=[[NSBundle mainBundle] pathForResource:@"btnRight" ofType:@"png"];
    [addButton setBackgroundImage:[UIImage imageWithContentsOfFile:filePath3] forState:UIControlStateNormal];
    [addButton addTarget:self action:@selector(addButtonPressd:) forControlEvents:UIControlEventTouchUpInside];
    
    //定义显示数字的label
    countLab = [[UILabel alloc] initWithFrame:CGRectMake(134, positionY, 44, 28)];
    countLab.font = [UIFont fontWithName:@"Helvetica-Bold" size:18];
    [countLab setText:[[NSString alloc] initWithFormat:@"%d",0]];
    countLab.textAlignment = UITextAlignmentCenter;
    countLab.adjustsFontSizeToFitWidth = YES;
    UIColor * color = [UIColor colorWithPatternImage:[UIImage imageNamed:@"btnMiddle.png"]]; 
    countLab.backgroundColor = color;
    
    
    [self.view addSubview:minusButton];
    [self.view addSubview:addButton];
    [self.view addSubview:countLab];
    [self.view addSubview:priceLab];
}

- (void)minuButtonPressd:(id)sender{
    NSURL *system_sound_url=[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"1248" ofType:@"wav"]];
    SystemSoundID system_sound_id;
    AudioServicesCreateSystemSoundID((__bridge CFURLRef)system_sound_url, &system_sound_id);
    AudioServicesPlaySystemSound(system_sound_id);
    
    int count = [countLab.text intValue];
    if(count > 0){
        count--;
    }
    countLab.text = [[NSString alloc] initWithFormat:@"%d",count];
}

- (void)addButtonPressd:(id)sender{
    NSURL *system_sound_url=[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"1248" ofType:@"wav"]];
    SystemSoundID system_sound_id;
    AudioServicesCreateSystemSoundID((__bridge CFURLRef)system_sound_url, &system_sound_id);
    AudioServicesPlaySystemSound(system_sound_id);
    
    int count = [countLab.text intValue];
    count++;
    countLab.text = [[NSString alloc] initWithFormat:@"%d",count];
}

-(void)setCountLabText:(NSInteger)number{
    [countLab setText:[[NSString alloc] initWithFormat:@"%d",number]];
}

-(void)setPriceLabText:(float)price{
    [priceLab setText:[[NSString alloc] initWithFormat:@"¥ %0.2f",price]];
}

- (void)viewDidUnload {
    self.countLab = nil;
    self.priceLab = nil;
    self.desc = nil;
    self.viewPosition = nil;
    [super viewDidUnload];
}

@end
